package com.zyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyx.pojo.User;
import com.zyx.service.UserService;
import com.zyx.mapper.UserMapper;
import com.zyx.util.JwtHelper;
import com.zyx.util.MD5Util;
import com.zyx.util.Result;
import com.zyx.util.ResultCodeEnum;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author 张杨煦
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-09-08 14:40:17
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(User user) {
        //根据用户名查询用户
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(wrapper);
        //判断用户是否存在
        if(loginUser == null){
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //判断密码是否正确
        if(!StringUtils.isEmpty(user.getUserPwd())
                && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())) {
            //登录成功
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            //将token返回给前端
            Map data = new HashMap();
            data.put("token", token);
            return Result.ok(data);
        }
        //密码错误
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }

    @Override
    public Result getUserInfo(String token) {
        //是否过期
        if(jwtHelper.isExpiration(token)){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        int userId = jwtHelper.getUserId(token).intValue();
        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        Map data = new HashMap();
        data.put("loginUser", user);
        return Result.ok(data);
    }

    @Override
    public Result checkUserName(String username) {
        //1.根据用户名查询用户
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        Long count = userMapper.selectCount(wrapper);
        //2.判断用户是否存在
        if (count == 0) {
            //不存在
            return Result.ok(null);
        }
        //存在
        return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
    }

    @Override
    public Result regist(User user) {
        //1.检查是否被注册
        if(checkUserName(user.getUsername()).getCode() == 200){
            //2.密码加密处理
            user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
            //3.保存用户信息
            userMapper.insert(user);
            return Result.ok(null);
        }
        return Result.build(null, ResultCodeEnum.USERNAME_USED);
    }

    @Override
    public Result checkLogin(String token) {
        if(jwtHelper.isExpiration(token))
            //已经过期了
            return Result.build(null, ResultCodeEnum.NOTLOGIN);

        return Result.ok(null);
    }
}




